<?php
@header("Content-Type:text/html;charset=utf-8");
@include_once("conn.php");

if(!(isset($_GET['user']) && isset($_GET['gid']) && isset($_GET['buynum']))){
    $obj = array();
    $obj["status"] = false;
    $obj["msg"] = '请输入完整的参数';
    $json = json_encode($obj);
    exit($json);
}

$user = $_GET['user'];
$gid = $_GET['gid'];
$buynum = $_GET['buynum'];

$sql = "select * from `shoppingchar` where user = '$user' and gid = '$gid'";

$result = mysqli_query($conn,$sql);
$obj = array();
if($result){
  
    $item = mysqli_fetch_assoc($result);
    if($item){
        $sql = "update `shoppingchar` set buynum = buynum + '$buynum' where user = '$user' and gid = '$gid'";
    }else{
        $sql = "insert into `shoppingchar`(user,gid,buynum) values('$user','$gid','$buynum')";
    }

    $result = mysqli_query($conn,$sql);
    if($result){

        $rows = mysqli_affected_rows($conn);

        // echo $rows;
        if($rows>0){ // 新增成功
            $obj["status"] = true;
            $obj["msg"] = "加入成功";
        }else{
            $obj["status"] = false;
            $obj["msg"] = "加入失败";
        }

    }else{
        $obj["status"] = false;
        $obj["msg"] = "sql语句有误";
        $obj["sql"] = $sql;
    }
}else{
    $obj["status"] = false;
    $obj["msg"] = "sql语句有误";
    $obj["sql"] = $sql;
    
}
echo json_encode($obj);
?> 